﻿package com.zhuangyzh.cmpp;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * 
 * 链路检查消息结构定义
 * 当SP收到ISMG发送的链路检测消息后，应以CMPP_ACTIVE_TEST_RESP消息回应。
 */
public class CMPPActiveTestResp extends CMPPHeader {

    private byte reserved = 0;//

    // 解析收到的ActiveTest消息，然后通过CMPPActiveTestResp返回。
    public CMPPActiveTestResp(byte[] data) {
        if (data.length == 8) {
            ByteArrayInputStream bins = new ByteArrayInputStream(data);
            DataInputStream dins = new DataInputStream(bins);
            try {
                this.setTotal_Length(data.length + 1);
                this.setCommand_Id(dins.readInt());
                this.setSequence_Id(dins.readInt());
                dins.close();
                bins.close();
            } catch (IOException e) {
            }
        } else {
            System.out.println("链路检查,解析数据包出错，包长度不一致。长度为:" + data.length);
        }
    }

    public byte getReserved() {
        return reserved;
    }

    public void setReserved(byte reserved) {
        this.reserved = reserved;
    }

    public byte[] toByteArry() {
        ByteArrayOutputStream bous = new ByteArrayOutputStream();
        DataOutputStream dous = new DataOutputStream(bous);
        try {
            dous.writeInt(this.getTotal_Length());
            dous.writeInt(this.getCommand_Id());
            dous.writeInt(this.getSequence_Id());
            dous.writeByte(this.getReserved());
            dous.close();
        } catch (IOException e) {
            System.out.println("封装链接二进制数组失败。");
        }
        return bous.toByteArray();
    }

}
